//
//  VideoPlayListTableViewCell.m
//  Dayou
//
//  Created by mac on 2018/8/14.
//  Copyright © 2018年 YangYang. All rights reserved.
//

#import "VideoPlayListTableViewCell.h"

#import <UIImageView+ZFCache.h>

@interface VideoPlayListTableViewCell()


@property (nonatomic, strong) UIImageView *headImageView;
@property (nonatomic, strong) UILabel *nickNameLabel;
@property (nonatomic, strong) UIImageView *coverImageView;
@property (nonatomic, strong) UIView *fullMaskView;
@property (nonatomic, strong) UIButton *playBtn;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) NSIndexPath *indexPath;
@property (nonatomic, assign) BOOL isVerticalVideo;
@property (nonatomic, assign) CGFloat videoHeight;

@property (nonatomic, weak) id <VideoPlayCellDelegate> delegate;

@end

@implementation VideoPlayListTableViewCell
-(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
//        WeakSelf;
        
        _headImageView = [[UIImageView alloc] init];
        [self.contentView addSubview:_headImageView];
        [_headImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.contentView.mas_left).mas_offset(10);
            make.top.mas_equalTo(self.contentView.mas_top).mas_offset(10);
            make.width.mas_equalTo(30);
            make.height.mas_equalTo(30);
        }];
        
        _nickNameLabel = [[UILabel alloc] init];
        [self.contentView addSubview:_nickNameLabel];
        _nickNameLabel.font = [UIFont systemFontOfSize:15];
        _nickNameLabel.textColor = UIColorFromRGB(0x333333);
        [_nickNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.headImageView.mas_right).mas_offset(10);
            make.centerY.mas_equalTo(self.headImageView.mas_centerY).mas_offset(0);
        }];
        
        _coverImageView = [[UIImageView alloc] init];
        [self.contentView addSubview:_coverImageView];
        _coverImageView.userInteractionEnabled = YES;
        _coverImageView.tag = 100;
        [_coverImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.contentView.mas_left).mas_offset(0);
            make.right.mas_equalTo(self.contentView.mas_right).mas_offset(0);
            make.top.mas_equalTo(self.headImageView.mas_bottom).mas_offset(5);
            make.height.mas_equalTo(0);
        }];
        
        _playBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_coverImageView addSubview:_playBtn];
        [_playBtn setImage:[UIImage imageNamed:@"play"] forState:UIControlStateNormal];
        [_playBtn addTarget:self action:@selector(playAction:) forControlEvents:UIControlEventTouchUpInside];
        [_playBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.mas_equalTo(self.coverImageView.mas_centerX).mas_offset(0);
            make.centerY.mas_equalTo(self.coverImageView.mas_centerY).mas_offset(0);
        }];
        
        _titleLabel = [[UILabel alloc] init];
        [self.contentView addSubview:_titleLabel];
        _titleLabel.font = [UIFont systemFontOfSize:15];
        _titleLabel.textColor = UIColorFromRGB(0x333333);
        _titleLabel.numberOfLines = 0;
        [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.contentView.mas_left).mas_offset(10);
            make.right.mas_equalTo(self.contentView.mas_right).mas_offset(-10);
            make.top.mas_equalTo(self.coverImageView.mas_bottom).mas_offset(5);
        }];
    }
    return self;
}

-(void)playAction:(UIButton *)sender
{
    if ([self.delegate respondsToSelector:@selector(playTheVideoAtIndexPath:)]) {
        [self.delegate playTheVideoAtIndexPath:self.indexPath];
    }
}

-(void)setModel:(CellModel *)model
{
    _model = model;
    [_headImageView setImageWithURLString:model.head placeholder:[UIImage imageNamed:@"defaultUserIcon"]];
    [_coverImageView setImageWithURLString:model.thumbnail_url placeholder:[UIImage imageNamed:@"loading_bgView"]];
    _nickNameLabel.text = model.nick_name;
    _titleLabel.text = model.title;
    
    CGSize titleSize = [model.title boundingRectWithSize:CGSizeMake(ScreenWidth - 20, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:15]} context:nil].size;
    [_titleLabel mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(titleSize.height);
    }];
    
    CGFloat videoHeight = [UIScreen mainScreen].bounds.size.width * self.model.video_height/self.model.video_width;
    
    [_coverImageView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(videoHeight);
    }];
    
    model.cellHeight = 10 + 30 + 5 + videoHeight + 5 + titleSize.height + 10;
}

-(void)setDelegate:(id<VideoPlayCellDelegate>)delegate withIndePath:(NSIndexPath *)indexPath
{
    self.delegate = delegate;
    self.indexPath = indexPath;
}

- (BOOL)isVerticalVideo {
    return _model.video_width < _model.video_height;
}

- (CGFloat)videoHeight {
    CGFloat videoHeight;
    if (self.isVerticalVideo) {
        videoHeight = [UIScreen mainScreen].bounds.size.width * 0.6 * self.model.video_height/self.model.video_width;
    } else {
        videoHeight = [UIScreen mainScreen].bounds.size.width * self.model.video_height/self.model.video_width;
    }
    return videoHeight;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
